#include "pipe_elements.h"
#include <string>
int CDB::add(int cycle, int result, int value, int robid, string op,int rsnumber)
{
    CDBmember *newcdb=new CDBmember;
    newcdb->cycle=cycle;
    newcdb->result=result;
    newcdb->value=value;
    newcdb->robid=robid;
    newcdb->optype=op;
    newcdb->rsnumber=rsnumber;
    tCDB.push_back(*newcdb);
    
}
int CDB::add(int cycle, int result, int value, int robid, string op,int l,int rsnumber)
{
    CDBmember *newcdb=new CDBmember;
    newcdb->cycle=cycle;
    newcdb->result=result;
    newcdb->value=value;
    newcdb->robid=robid;
    newcdb->optype=op;
    newcdb->loadcounter=l;
    newcdb->rsnumber=rsnumber;
    tCDB.push_back(*newcdb);

}
CDBmember CDB::getnext(int cycle)
{
    if(!tCDB.empty())
    {
    CDBmember temp=tCDB.front();
    if(temp.cycle<cycle||!temp.optype.compare("SW"))
        {
         tCDB.pop_front();
         return temp;
        }
    }
    return *(new CDBmember);
}
int CDB::empty(int cycle)
{
    deque <CDBmember>::iterator it;
    for(it=tCDB.begin();it!=tCDB.end();++it)
    {
        if((*it).cycle<cycle)
            return 1;
    }
    return 0;
}

void CDB::fprint()
{
    cout<<"CDB:\n";
    deque <CDBmember>::iterator it;
    for(it=tCDB.begin();it!=tCDB.end();++it)
    {
        cout<<"["<<(*it).optype<<","<<(*it).robid<<", "<<(*it).cycle<<"]";

    }
}
void CDB::flush()
{
    tCDB.clear();
    
}